#coding=utf-8
## site information setting
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from com.jumang.model.SystemConfig import SystemConfig
from google.appengine.ext import db

class JumConfig():
    mainTitle=u'天涯工作间'
    mainDescription=u'Jumang\'s web site. I like C++ and Java, but I like python now.'
    mainKeyword=u'java,Jumang,skyline,C++,GAE,python,web'
    siteTitle=u'skyline\'s workplace'
    #local test
#    currentHome=u'http://jum1.appspot.com/'
#    siteHome=u'http://jum1.appspot.com/'
#    resourceHome=u'http://jum2.appspot.com/'
#    albumHome=u'http://jum3.appspot.com/'
#    privateHome=u'http://jum4.appspot.com/'
    #public
    currentHome=u'http://jum.appspot.com/'
    siteHome=u'http://jum.appspot.com/'
    resourceHome=u'http://jumres.appspot.com/'
    albumHome=u'http://jumpics.appspot.com/'
    privateHome=u'http://jumangs.appspot.com/'
    #test
#    currentHome=u'http://skylinezcn.appspot.com/'
#    siteHome=u'http://skylinezcn.appspot.com/'
#    resourceHome=u'http://51skyline.appspot.com/'
#    albumHome=u'http://asafather.appspot.com/'
#    privateHome=u'http://fastmask.appspot.com/'
    
    siteDescription=siteTitle
    logoPath=u'/res/image/logo.png'
    pageSize=10
    displayWidth=420
    defaultUserName=u'Jumang'
    defaultPassword=u'postpassword'
    defaultPrivatePwd=u'privatepwd'
    defaultLinks='''
          <ul>
            <li><a href="http://magrie.bokee.com/" target="_blank">秋虫</a></li>
            <li><a href="http://fastmask.blogspot.com/" target="_blank">skyline thinking</a></li>
            <li><a href="http://skyline.bokee.com/" target="_blank">天涯的思考</a></li>
            <li><a href="http://blog.csdn.net/fastmask" target="_blank">天涯的思考</a></li>
            <li><a href="http://skylinezcn.appspot.com/" target="_blank">skyline's workspace</a></li>
            <li><a href="http://jum.appspot.com/" target="_blank">skyline's workspace</a></li>
            <li><a href="http://plog.appspot.com/" target="_blank"> 小七的破锣歌</a></li>
            <li><a href="http://onlypython.appspot.com/" target="_blank"> Python主题博客网站</a></li>
            <li><a href="http://china-app-blog.appspot.com/" target="_blank"> China appspot blog</a></li>
            <li><a href="http://edikudweb.appspot.com/" target="_blank"> Edikud</a></li>
          </ul>
          '''
    linksName='links'
    userNameName='username'
    passwordName='postpassword'
    privatePwdName='privatepwd'
    
    def getLinks(self,request):
      currentHost='http://'+request.environ['HTTP_HOST']+'/'
      links=memcache.get(self.linksName)
      if links:
        return links
      
      if currentHost==self.siteHome:
        query=SystemConfig.gql("where name=:name",name=self.linksName)
        data=query.fetch(1)
        if data:
          config=data[0]
        else:
          config=SystemConfig(name=unicode(self.linksName,'utf-8'),
                              value=db.Text(self.defaultLinks,'utf-8'))
          config.put()
        memcache.set(self.linksName,config.value)
      else:
        mainUrl=self.siteHome+'main/config/'
        success=True
        try:
          result=urlfetch.fetch(mainUrl)
        except:
          success=False
        query=SystemConfig.gql("where name=:name",name=self.linksName)
        data=query.fetch(1)
        if success and result.status_code == 200:
          if data:
            config=data[0]
            config.value=db.Text(result.content,'utf-8')
            config.put()
          else:
            config=SystemConfig(name=unicode(self.linksName,'utf-8'),
                              value=db.Text(result.content,'utf-8'))
            config.put()
        else:
          if data:
            config=data[0]
          else:
            config=SystemConfig(name=unicode(self.linksName,'utf-8'),
                              value=db.Text(self.defaultLinks,'utf-8'))
            config.put()
          
        memcache.set(self.linksName,config.value,3600)
      return config.value
    
    def getParameter(self,name,value):
      query=SystemConfig.gql("where name=:name",name=name)
      data=query.fetch(1)
      if data:
        config=data[0]
      else:
        config=SystemConfig(name=unicode(name,'utf-8'),
                            value=db.Text(value))
        config.put()
      return config.value
      
    
    def getPostPassword(self):
      return self.getParameter(self.passwordName,self.defaultPassword)
      
    def getPrivatePwd(self):
      return self.getParameter(self.privatePwdName,self.defaultPrivatePwd)
      
    def getUserName(self):
      return self.getParameter(self.userNameName,self.defaultUserName)
      
realConfig=JumConfig()